package com.scommunity.admin.controller;

import com.github.pagehelper.PageInfo;
import com.scommunity.common.ResponseResult;
import com.scommunity.domain.Parking;
import com.scommunity.service.IParkingService;
import com.scommunity.utils.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/parking")
public class ParkingController{

    @Autowired
    private IParkingService iParkingService;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    @PostMapping("/add")
    public Object add(@RequestBody Parking parking){
        //String parkingNum = "scpark" + parking.getParkingId();
        long parkingId = snowflakeIdWorker.nextId();
        parking.setParkingId(parkingId);
       // parking.setParkingNum(parkingNum);
        Parking nparking = iParkingService.add(parking);
        return ResponseResult.success(nparking);
    }

    @PostMapping("/delete")
    public void delete(@RequestBody Parking parking){
        iParkingService.delete(parking);
    }

    @PostMapping("/update")
    public void update(@RequestBody Parking parking){
        iParkingService.updateN(parking);
    }

    @GetMapping("/all")
    public Object all(Integer page){
        if (page == null) {
            page = 1;
        }
        Parking parking = new Parking();
        PageInfo<Parking> pageInfo = iParkingService.all(page,8);
        if(pageInfo == null){
            return ResponseResult.success();
        }else{
            return ResponseResult.success(pageInfo);
        }
    }

    @PostMapping("/findByCondition")
    public Object findByCondition(@RequestBody Parking parking){
        PageInfo<Parking> pageInfo = iParkingService.getPageByCondition(parking,1,8);
        if(pageInfo == null){
            return ResponseResult.success();
        }else{
            return ResponseResult.success(pageInfo);
        }
    }
}

